package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.enviaEmail;
import bean.OSBean;
import conexao.Conexao;
import dao.OSDao;

public class enviarImprimirServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public enviarImprimirServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");

		OSBean osBean = new OSBean();
		osBean.setId(Integer.parseInt(request.getParameter("id")));
		osBean.setDataAprovado(new Timestamp(System.currentTimeMillis()));
		osBean.setDataImprimir(new Timestamp(System.currentTimeMillis()));

		Connection conn = null;

		try {
			conn = new Conexao().getConexao();

			new OSDao().enviaOSImprimir(conn, osBean);
			conn.commit();

		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
				ex.printStackTrace();
			}
			new enviaEmail().enviar(e);
			throw new RuntimeException(e);
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
			}
		}
	}

}
